package com.myproject.myblogserver.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;

import com.myproject.myblogserver.contant.CommonConstants;
import com.myproject.myblogserver.entity.FavoriteFolder;
import com.myproject.myblogserver.entity.FolderSubscription;
import com.myproject.myblogserver.service.IFavoriteFolderService;
import com.myproject.myblogserver.service.IFolderSubscriptionService;
import com.myproject.myblogserver.utils.JwtUtil;

/**
 * <p>
 * 收藏夹订阅表 前端控制器
 * </p>
 *
 * @author jiangweike
 * @since 2025-02-16
 */
@Controller
@RequestMapping("/folderSubscription")
public class FolderSubscriptionController {

    @Autowired
    private IFolderSubscriptionService folderSubscriptionService;

    @Autowired
    private IFavoriteFolderService favoriteFolderService;

    /**
     * 添加收藏夹订阅
     */
    @PostMapping("/subscribe/{folderId}")
    public ResponseEntity<?> subscribe(@PathVariable Long folderId,
            @RequestHeader("Authorization") String authHeader) {
        try {
            var userId = JwtUtil.extractUserIdFromAuthHeader(authHeader);

            // 检查收藏夹是否存在
            var folder = favoriteFolderService.lambdaQuery()
                    .eq(FavoriteFolder::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .eq(FavoriteFolder::getId, folderId)
                    .one();

            if (folder == null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("收藏夹不存在！");
            }

            // 检查是否已经订阅
            var existingSubscription = folderSubscriptionService.lambdaQuery()
                    .eq(FolderSubscription::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .eq(FolderSubscription::getUserId, userId)
                    .eq(FolderSubscription::getFolderId, folderId)
                    .one();

            if (existingSubscription != null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("已经订阅过该收藏夹！");
            }

            // 创建新的订阅记录
            var subscription = new FolderSubscription();
            subscription.setUserId(userId);
            subscription.setFolderId(folderId);
            folderSubscriptionService.save(subscription);

            return ResponseEntity.ok("订阅收藏夹成功！");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("订阅收藏夹失败！");
        }
    }

    /**
     * 删除收藏夹订阅
     */
    @PostMapping("/unsubscribe/{folderId}")
    public ResponseEntity<?> unsubscribe(@PathVariable Long folderId,
            @RequestHeader("Authorization") String authHeader) {
        try {
            var userId = JwtUtil.extractUserIdFromAuthHeader(authHeader);

            // 查找订阅记录
            var subscription = folderSubscriptionService.lambdaQuery()
                    .eq(FolderSubscription::getIsDeleted, CommonConstants.DELETED_STATUS_NO)
                    .eq(FolderSubscription::getUserId, userId)
                    .eq(FolderSubscription::getFolderId, folderId)
                    .one();

            if (subscription == null) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("未找到订阅记录！");
            }

            // 软删除订阅记录
            folderSubscriptionService.lambdaUpdate()
                    .set(FolderSubscription::getIsDeleted, CommonConstants.DELETED_STATUS_YES)
                    .eq(FolderSubscription::getId, subscription.getId())
                    .update();

            return ResponseEntity.ok("取消订阅成功！");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("取消订阅失败！");
        }
    }
}
